Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support # comments in regex #3735

Merged
merged 3 commits into from
Dec 17, 2024
Merged

Support # comments in regex #3735

merged 3 commits into from
Dec 17, 2024

Conversation

staabm
Copy link
Contributor

@staabm staabm commented Dec 16, 2024

closes phpstan/phpstan#12242

see https://www.regular-expressions.info/freespacing.html#freecomment

will look into whitespace normalization for the x-modifer in a separate PR

@phpstan-bot
Copy link
Collaborator

This pull request has been marked as ready for review.

Comment on lines 75 to 76
// The regex engine ignores everything after the (?# until the first closing parenthesis
$regex = preg_replace('/\(\?#[^)]*\)/', '', $regex) ?? '';
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

general comments which work independent from modifiers

@ondrejmirtes ondrejmirtes merged commit c053dbc into phpstan:1.12.x Dec 17, 2024
452 checks passed
@ondrejmirtes
Copy link
Member

Thank you!

$modifiers = $this->regexExpressionHelper->getPatternModifiers($regex) ?? '';
foreach (self::NOT_SUPPORTED_MODIFIERS as $notSupportedModifier) {
if (str_contains($modifiers, $notSupportedModifier)) {
return null;
}
}

if (str_contains($modifiers, 'x')) {
// in freespacing mode the # character starts a comment and runs until the end of the line
$regex = preg_replace('/[^?]#.*/', '', $regex) ?? '';
Copy link
Contributor

@Seldaek Seldaek Dec 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will also get rid of the character before # due to [^?]. I realize in most sane cases this will be a space and thus it's harmless, but IMO this should rather be a lookbehind like (?<!\?). I assume the goal was to exclude (?#...) but those are already ignored/handled by the lexer https://github.com/phpstan/phpstan-src/blob/2.0.x/resources/RegexGrammar.pp#L80-L82

Copy link
Contributor Author

@staabm staabm Dec 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will look into it, thanks for the heads up.

(I had to use this additional char before the # as we would otherwise destroy comments in (?# ..) notation). since we replace before lexing we need to make sure we don't turn the comments into something which is no longer a comment

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fix in #3739

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants